<!--  -->
<template>
  <div class="ruleform">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="ruleForm.role" placeholder="请选择">
          <el-option v-for="item in role" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码" v-if="!this.ruleForm.id">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入的其他文件 例如：import moduleName from 'modulePath';
import { roleList, userSave } from "@/api/rightsManagement";
export default {
  //import所引入的组件注册
  components: {},

  data() {
    return {
      role: [],
      orgOpiEdit: false,
      ruleForm: {
        name: "",
        userName: "",
        phone: "",
        role: "",
        id: "",
        password: ""
      },
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        confirmPass: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ]
      }
    };
  },

  //监听属性
  computed: {},

  //监控data中的数据变化
  watch: {},

  //方法集合
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        console.log(valid);
        if (valid) {
          if (this.ruleForm.id) {
            const data = {
              id: this.ruleForm.id,
              name: this.ruleForm.name,
              userName: this.ruleForm.userName,
              phone: this.ruleForm.phone
            };
            userSave(data).then(res => {
              console.log(res);
              if (res.code == 20000) {
                this.$message({ type: "success", message: res.msg });
                this.$router.go(-1); 
              } else {
                this.$message({ type: "warning", message: res.msg });
              }
            });
          } else {
            userSave({ ...this.ruleForm }).then(res => {
              console.log(res);
              if (res.code == 20000) {
                this.$message({ type: "success", message: res.msg });
                this.$router.go(-1); 
              } else {
                this.$message({ type: "warning", message: res.msg });
              }
            });
          }
        } else {
            console.log("error submit!!");
            return false;
        }
      });
    },
    init() {
      console.log(this.$route.query, 222);
      this.ruleForm = this.$route.query;
      roleList().then(res => {
        console.log(res);
        this.role = res.rows;
      });
    }
  },

  //生命周期 - 组件实例刚被创建
  beforeCreate() {},
  //创建完成 访问当前this实例
  created() {},
  //挂载之前
  beforeMount() {},
  //挂载完成 访问DOM元素
  mounted() {
    this.init();
  }
};
</script>
<style lang='scss' scoped>
/* @import url(); 引入公共css类 */
.ruleform {
  padding: 40px;
}
</style>